'use strict';

angular.module('myApp.index', ['ngRoute', 'pascalprecht.translate'])

  .config(['$routeProvider', '$translateProvider', function ($routeProvider, $translateProvider) {
    $routeProvider.when('/index', {
      templateUrl: 'pages/index/index.html',
      controller: 'IndexCtrl'
    });
    $routeProvider.when('/', {
      templateUrl: 'pages/index/index.html',
      controller: 'IndexCtrl'
    });
  }])

  .controller('IndexCtrl', function ($http, $rootScope, $scope, $location, $translate) {

    const items = [
      {
        link: '#!/explore',
        src: `assets/themes/${iconTheme}/icon-folder.png`,
        name: '文件管理'
      },
//       {
//         link: '#!/testing',
//         src: `assets/themes/${iconTheme}/icon-testing.png`,
//         name: '内置测试'
//       },
      {
        link: '#!/app',
        src: `assets/themes/${iconTheme}/icon-app-3.png`,
        name: '应用程序'
      },
//       {
//         link: '#!/setting',
//         src: `assets/themes/${iconTheme}/icon-setting.png`,
//         name: '系统设置'
//       }
    ]
    $rootScope.title = '行空板'
    // $rootScope.ip_address = ''
    $rootScope.items = items
    $rootScope.rowNum = 3
    $rootScope.colNum = 2
    $rootScope.localMenus[$location.path()] = [
      {
        text: '重新载入',
        callback: (index) => {
          window.location.reload(true);
        }
      },
      {
        text: '关机',
        callback: (index) => {
          console.log(`menu item-${index} clicked`)
          swal.fire({
            title: translate("正在关机"),
            showConfirmButton: false,
          });
          axios.get('/system/halt')
          // window.ros.systemPoweroff().then(() => {
          //   console.log('systemPoweroff responsed')
          //   clearTimeout(timer)
          // })
        }
      }, {
        text: '重启',
        callback: (index) => {
          console.log(`menu item-${index} clicked`)
          swal.fire({
            title: translate("正在准备重启"),
            showConfirmButton: false,
          });
          $http.get('/system/reboot')
        }
      }, {
        text: 'Switch to English',
        callback: (index) => {
          console.log(`menu item-${index} clicked`)
          $translate.use('en');
          $http.get('/system/set_env?key=lang&value=en')
        },
      }, {
        text: '切换中文',
        callback: (index) => {
          console.log(`menu item-${index} clicked`)
          $translate.use('zh');
          $http.get('/system/set_env?key=lang&value=zh')
        }
      }
    ]

    console.log('index entered')
    $rootScope.updatePageInfo()

    var updatePageData = () => {
      $http.get('/system/ipInfo').then(res => {
        let ip_address = ''
        res.data.map(net => {
          if (net.interface == 'wlan0') {
            ip_address = `[${net.ip}]`
          }
        })
        res.data.map(net => {
          if (net.interface == 'eth0') {
            ip_address = `[${net.ip}]`
          }
        })
        res.data.map(net => {
          if (net.interface == 'usb0') {
            ip_address = `[${net.ip}]`
          }
        })
        res.data.map(net => {
          if (net.interface == 'br0') {
            ip_address = `[${net.ip}]`
          }
        })
        if ($location.path() == '/index' || $location.path() == '/') {
          if ($rootScope.ip_address != ip_address) {
            // $scope.ip_address = ip_address
            $rootScope.ip_address = ip_address
            ngRefresh()
          }
          setTimeout(updatePageData, 3000)
        }
      })
    }

    $rootScope.onNewMsg = (msg) => {
      if ($location.path() == '/index' || $location.path() == '/') {
        if (msg.msg && msg.from != 'localhost') {
          swal.fire({
            title: translate("新消息"),
            text: `${msg.from}:\n${msg.msg}`,
            // showConfirmButton: true,
            showCancelButton: true,
            cancelButtonText: "关闭",
      });
        }
      }
    }

    updatePageData()
    $scope.$on("$destroy", function () {
      $rootScope.ip_address = ''
    })

    async function setAudioDevice(){
      const devices = await navigator.mediaDevices.enumerateDevices();
      console.log(devices)
      const audioDevices = devices.filter((device) => device.kind === "audioinput");
      if(audioDevices.length == 2){
        window.audio_constraints = { audio: { deviceId: { exact: audioDevices[1].deviceId } } }
      }
    }

    setAudioDevice()

  });